package com.ztt.vxchat.utils;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ztt
 * @project_name vx-chat
 * @description []
 * @date 2022/8/24 15:11
 */
@Data
@Accessors(chain = true)
public class SwitchInfoUtil {




    String weatherCity;
    String weatherDate;
    String weatherWeek;
    String weatherType;
    String weatherHigh;
    String weatherLow;
    String weatherFengxiang;
    String weatherFengli;
    String weatherTip;


    String fortuneAll;
    String fortuneLove;
    String fortuneWork;
    String fortuneMoney;
    String fortuneLuckycolor;
    String fortuneLuckynumber;
    String fortuneLuckyconstellation;
    String fortuneShortcomment;
    String fortuneAllStr;
    String fortuneLoveStr;
    String fortuneWorkStr;
    String fortuneMoneyStr;
    String fortuneHealthStr;






    //    获取天气
    public JSONObject getWeather(String city){
//        根据城市获取天气信息

        String url = "https://api.vvhan.com/api/weather";

        Map<String,Object> params = new HashMap<String,Object>();
        params.put("city",city);

        String result = HttpRequest.get(url)
                .header(Header.USER_AGENT, "ztt")//头信息，多个头信息多次调用此方法即可
                .form(params)
                .timeout(20000)//超时，毫秒
                .execute().body();

        JSONObject resultJson = JSONUtil.parseObj(result);
        JSONObject infoJson = resultJson.getJSONObject("info");

        this.setWeatherCity(resultJson.getStr("city"));
        this.setWeatherDate(infoJson.getStr("date"));
        this.setWeatherWeek(infoJson.getStr("week"));
        this.setWeatherType(infoJson.getStr("type"));
        this.setWeatherHigh(infoJson.getStr("high").split(" ")[1]);
        this.setWeatherLow(infoJson.getStr("low").split(" ")[1]);
        this.setWeatherFengxiang(infoJson.getStr("fengxiang"));
        this.setWeatherFengli(infoJson.getStr("fengli"));
        this.setWeatherTip(infoJson.getStr("tip"));


        return resultJson;
    }


    //    一句情话
    public static String getLove(){

        String url = "https://api.vvhan.com/api/love";

        Map<String,Object> params = new HashMap<String,Object>();

        String result = HttpRequest.get(url)
                .header(Header.USER_AGENT, "ztt")//头信息，多个头信息多次调用此方法即可
                .form(params)
                .timeout(20000)//超时，毫秒
                .execute().body();

        return result;
    }

    //    一句笑话
    public static String getJoke(){

        String url = "https://api.vvhan.com/api/joke";

        Map<String,Object> params = new HashMap<String,Object>();

        String result = HttpRequest.get(url)
                .header(Header.USER_AGENT, "ztt")//头信息，多个头信息多次调用此方法即可
                .form(params)
                .timeout(20000)//超时，毫秒
                .execute().body();

        return result;
    }

    //    一句励志的话
    public static String getEn(){

        String url = "https://api.vvhan.com/api/en";

        Map<String,Object> params = new HashMap<String,Object>();
        params.put("type", "sj");

        String result = HttpRequest.get(url)
                .header(Header.USER_AGENT, "ztt")//头信息，多个头信息多次调用此方法即可
                .form(params)
                .timeout(20000)//超时，毫秒
                .execute().body();

        return JSONUtil.parseObj(result).getJSONObject("data").get("zh").toString();
    }


    //    一句骚话
    public static String getSao(){

        String url = "https://api.vvhan.com/api/sao";

        Map<String,Object> params = new HashMap<String,Object>();

        String result = HttpRequest.get(url)
                .header(Header.USER_AGENT, "ztt")//头信息，多个头信息多次调用此方法即可
                .form(params)
                .timeout(20000)//超时，毫秒
                .execute().body();

        return result;
    }



//    获取一个日期到今天有多少天
    public static String getBeforeNowDay(String before){

        String result = "";

        Date beforeDate = DateUtil.parse(before);
        Date nowDate = DateUtil.parse(DateUtil.now());

        long betweenDay = DateUtil.between(beforeDate, nowDate, DateUnit.DAY);


        result = String.valueOf(Math.abs(betweenDay));

        return result;
    }





    //    今日星座运势
    public JSONObject getFortune(String horoscope){

        String url = "https://api.vvhan.com/api/horoscope";

        Map<String,Object> params = new HashMap<String,Object>();
        params.put("type", getConstellation(horoscope));
        params.put("time", "today");

        String result = HttpRequest.get(url)
                .header(Header.USER_AGENT, "ztt")//头信息，多个头信息多次调用此方法即可
                .form(params)
                .timeout(20000)//超时，毫秒
                .execute().body();

        JSONObject resultJson = JSONUtil.parseObj(result);
        JSONObject dataJson = resultJson.getJSONObject("data");
        JSONObject fortuneJson = dataJson.getJSONObject("fortune");
        JSONObject fortuneTextJson = dataJson.getJSONObject("fortunetext");

        this.setFortuneAll(fortuneJson.getStr("all"));
        this.setFortuneLove(fortuneJson.getStr("love"));
        this.setFortuneWork(fortuneJson.getStr("work"));
        this.setFortuneMoney(fortuneJson.getStr("money"));
        this.setFortuneLuckycolor(dataJson.getStr("luckycolor"));
        this.setFortuneLuckynumber(dataJson.getStr("luckynumber"));
        this.setFortuneLuckyconstellation(dataJson.getStr("luckyconstellation"));
        this.setFortuneShortcomment(dataJson.getStr("shortcomment"));
        this.setFortuneAllStr(fortuneTextJson.getStr("all"));
        this.setFortuneLoveStr(fortuneTextJson.getStr("love"));
        this.setFortuneWorkStr(fortuneTextJson.getStr("work"));
        this.setFortuneMoneyStr(fortuneTextJson.getStr("money"));
        this.setFortuneHealthStr(fortuneTextJson.getStr("health"));


        return resultJson;
    }




//    转换星座
    public static String getConstellation(String zh) {

        if(StrUtil.hasBlank(zh)){
            return "sagittarius";
        }

        switch (zh){
            case "白羊":
                return "aries";
            case "金牛":
                return "taurus";
            case "双子":
                return "gemini";
            case "巨蟹":
                return "cancer";
            case "狮子":
                return "leo";
            case "处女":
                return "virgo";
            case "天枰":
                return "libra";
            case "天蝎":
                return "scorpio";
            case "射手":
                return "sagittarius";
            case "摩羯":
                return "capricorn";
            case "水平":
                return "aquarius";
            case "双鱼":
                return "pisces";
            default:
                return "sagittarius";
        }
    }



}
